package com.gxa.p2p.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gxa.p2p.mapper.BankCardMapper;
import com.gxa.p2p.mapper.UserInfoMapper;
import com.gxa.p2p.pojo.BankCard;
import com.gxa.p2p.service.BankCardService;
import com.gxa.p2p.util.Authentic;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class BankCardServiceImpl implements BankCardService {

    @Resource
    BankCardMapper bankCardMapper;

    // 用户信息
    @Resource
    UserInfoMapper userInfoMapper;
    @Override
    public Boolean saveBankCard(BankCard bankCard) throws TencentCloudSDKException {
        JSONObject jsonObject = Authentic.authrenBankCard(bankCard.getName(), bankCard.getBankAccount(), bankCard.getPhoneNumber(), bankCard.getIdCard());
        String result = (String) jsonObject.get("Result");
        System.out.println(jsonObject);
        boolean flag = false;
        // 拓展（没做）： 判断用户是否进行实名认证，若认证了其认证信息是否与银行卡信息一致，若不存在则将实名信息根据银行信息进行认证
        if ("0".equals(result)){
            // 验证银行卡信息是否正确
            if(userInfoMapper.selectByUserIdAndName(bankCard.getUserId(),bankCard.getName()) != null){
                // 验证与系统中的实名信息是否一致
                if(bankCardMapper.insertSelective(bankCard)>0){
                    flag=true;
                }
            }
        }
        // 银行卡信息验证成功
        return flag;
//        return bankCardMapper.insertSelective(bankCard) > 0 ;
    }

    @Override
    public List<BankCard> findUserCard(Integer id) {
        return bankCardMapper.selectByPrimaryKey(id);
    }

    /**
     * 银行卡选择方法
     * @param userId 需要用户编号
     * @return 返回该用户所有银行卡对象
     */
    @Override
    public List<BankCard> checkBankCard(Integer userId) {
        return bankCardMapper.selectByUserId(userId);

    }
}
